package com.ym.medical.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ym.core.utils.tkmybatis.BaseSVImpl;
import com.ym.medical.dao.PlatformTradeDetailDao;
import com.ym.medical.domain.entity.PlatformAccountEntity;
import com.ym.medical.domain.entity.PlatformTradeDetailEntity;
import com.ym.medical.domain.param.PlatformTradeDetailParam;
import com.ym.medical.enums.CodeRuleEnum;
import com.ym.medical.enums.ComputeWayEnum;
import com.ym.medical.service.IPlatformAccountSV;
import com.ym.medical.service.IPlatformTradeDetailSV;
import com.ym.medical.service.ISysCodeRuleSV;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * ComputerName:LAPTOP-I6GKDIFQ
 * Author:admin
 * Date: 2019-08-17
 * Time: 11:10:41
 * 服务实现类
 */
@Service
@Transactional( rollbackFor = Exception.class)
public class PlatformTradeDetailSVImpl extends BaseSVImpl<PlatformTradeDetailEntity,PlatformTradeDetailDao> implements IPlatformTradeDetailSV {

    @Autowired
    private IPlatformAccountSV platformAccountSV;

    @Autowired
    private ISysCodeRuleSV generateCodeRuleSV;

    @Override
    public void insertPlaformTradeDetail(String computeWay,BigDecimal tdAmount,Long relId,String tdBody,String tdType,String cashFlow) {
        PlatformAccountEntity platformAccount = platformAccountSV.selectById(1L);
        if(ComputeWayEnum.INCREASE.getKey().equals(computeWay)){
            platformAccount.setAccBalance(platformAccount.getAccBalance().add(tdAmount));
            platformAccount.setLastIncomeTime(new Date());
            platformAccount.setLastIncomeAmt(tdAmount);
            platformAccount.setTotalIncomeNum(platformAccount.getTotalIncomeNum()+1);
            platformAccount.setTotalIncomeAmt(platformAccount.getTotalIncomeAmt().add(tdAmount));
        }else if(ComputeWayEnum.TO_REDUCE.getKey().equals(computeWay)){
            platformAccount.setAccBalance(platformAccount.getAccBalance().subtract(tdAmount));
        }
        platformAccountSV.updateById(platformAccount);
        PlatformTradeDetailEntity platformTradeDetail = new PlatformTradeDetailEntity();
        platformTradeDetail.setPlatformAccId(platformAccount.getId());
        platformTradeDetail.setAftTdAmount(platformAccount.getAccBalance());
        platformTradeDetail.setTdTime(new Date());
        platformTradeDetail.setCreateTime(new Date());

        platformTradeDetail.setRelId(relId);
        //生成流水号
        platformTradeDetail.setTradeNo(generateCodeRuleSV.createCode(CodeRuleEnum.TRANSACTION_SERIAL_NUMBER));
        if(StringUtils.isEmpty(tdBody)){
            if(ComputeWayEnum.INCREASE.getKey().equals(computeWay)){
                tdBody="用户支付费用给平台";
            }else  if(ComputeWayEnum.TO_REDUCE.getKey().equals(computeWay)){
                tdBody="平台退费给用户";
            }
        }
        platformTradeDetail.setTdBody(tdBody);
        platformTradeDetail.setTdAmount(tdAmount);
        platformTradeDetail.setTdType(tdType);
        platformTradeDetail.setCashFlow(cashFlow);
        this.insert(platformTradeDetail);
    }

    @Override
    public PageInfo<PlatformTradeDetailEntity> listPagePlatformTdDetail(PlatformTradeDetailParam platformTradeDetailModel, int pageNo, int limit) {
        PageHelper.startPage(pageNo,limit);
        return new PageInfo<>(listPlatformTdDetail(platformTradeDetailModel));
    }

    @Override
    public List<PlatformTradeDetailEntity> listPlatformTdDetail(PlatformTradeDetailParam platformTradeDetailModel) {
        return mapper.listPlatformTdDetail(platformTradeDetailModel);
    }
}
